﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Web;
using Microsoft.SharePoint;

namespace SPAzureBlobField.Classes
{
    public class SPAzureBlobControl : TextField
    {
        protected override string DefaultTemplateName
        {
            get
            {
                if (this.ControlMode == SPControlMode.Display)
                {
                    return this.DisplayTemplateName;
                }
                else
                {
                    return "AzureBlobControl";
                }
            }
        }

        public override string DisplayTemplateName
        {
            get
            {
                return "AzureBlobControlForDisplay";
            }
            set
            {
                base.DisplayTemplateName = value;
            }
        }

        protected override void CreateChildControls()
        {
            // Registering javascript for the page.

            this.Page.ClientScript.RegisterClientScriptInclude(this.Page.GetType(), "jQuery151",
                this.Web.Url + "/_layouts/SPAzureBlobField/js/jquery-1.5.1.js");

            this.Page.ClientScript.RegisterClientScriptInclude(this.Page.GetType(), "azureBlobJS",
                this.Web.Url + "/_layouts/SPAzureBlobField/js/include.js");

            if (this.Field != null)
            {
                base.CreateChildControls();
            }
            if (!this.Page.IsPostBack)
            {
                string serviceUrl = this.Web.Url + "/_vti_bin/AzureBlobServices/AzureBlobService.svc";
                System.Web.UI.ScriptManager scriptManager = System.Web.UI.ScriptManager.GetCurrent(this.Page);
                scriptManager.Services.Add(new System.Web.UI.ServiceReference(serviceUrl));

                switch (this.ControlMode)
                {
                    case SPControlMode.New:
                        if (!this.Page.IsPostBack)
                        {
                            this.textBox = (TextBox)TemplateContainer.FindControl("textField");
                            var lnkDownloadBlob = (HyperLink)TemplateContainer.FindControl("lnkDownloadBlob");
                            lnkDownloadBlob.Visible = false;
                        }
                        break;
                    case SPControlMode.Edit:
                        if (!this.Page.IsPostBack)
                        {
                            EnsureChildControls();
                            this.textBox = (TextBox)TemplateContainer.FindControl("textField");
                            var lblInsertNoUpload = (Label)TemplateContainer.FindControl("lblInsertNoUpload");
                            var lnkDownloadBlob = (HyperLink)TemplateContainer.FindControl("lnkDownloadBlob");
                            lblInsertNoUpload.Visible = false;
                            if (!CheckIfCurrentlyUpdated())
                            {
                                lnkDownloadBlob.Attributes.Add("onclick",
                                    String.Format("openSLClientIfRequired('{0}','{1}','{2}','{3}','{4}','true')",
                                    this.Web.Url,
                                    this.ListId.ToString(),
                                    this.ListItem.ID.ToString(),
                                    this.Field.Id.ToString(),
                                    HttpUtility.UrlEncode(this.ListItem.GetValue(this.Field))
                                    ));
                            }
                            else
                            {
                                // Chaning link label to show it is not available.
                                lnkDownloadBlob.Text = "Not Available (somebody else is uploading).";
                                lnkDownloadBlob.Enabled = false;
                            }
                        }
                        break;
                    case SPControlMode.Display:
                        if (!this.Page.IsPostBack)
                        {
                            EnsureChildControls();
                            string value = this.ListItem.GetValue(this.Field);
                            var lnkDownloadBlobDisplay = (HyperLink)TemplateContainer.FindControl("lnkDownloadBlobDisplay");
                            var lnkShareToExternal = (HyperLink)TemplateContainer.FindControl("lnkShareToExternal");
                            if (value != String.Empty)
                            {
                                lnkDownloadBlobDisplay.Attributes.Add("onclick",
                                    String.Format("openSLClientIfRequired('{0}','{1}','{2}','{3}','{4}', 'false')",
                                    this.Web.Url,
                                    this.ListId.ToString(),
                                    this.ListItem.ID.ToString(),
                                    this.Field.Id.ToString(),
                                    HttpUtility.UrlEncode(this.ListItem.GetValue(this.Field))
                                    ));
                                lnkShareToExternal.Attributes.Add("onclick",
                                    String.Format("openShareLinkMail('{0}','{1}','{2}','{3}','{4}')",
                                    this.Web.Url,
                                    this.ListId.ToString(),
                                    this.ListItem.ID.ToString(),
                                    this.Field.Id.ToString(),
                                    HttpUtility.UrlEncode(this.ListItem.GetValue(this.Field))
                                    ));
                            }
                            else
                            {
                                // Chaning link label to show it is not available.
                                lnkDownloadBlobDisplay.Text = "Not Available (no content added).";
                                lnkDownloadBlobDisplay.Enabled = false;
                            }
                        }
                        break;
                }
            }
        }

        private bool CheckIfCurrentlyUpdated()
        {
            return AzureBrowserConnections.IsBLobCUrrentlyUpdated(this.Web.Site.ID, this.Web.ID, this.List.ID, this.ItemId);
        }

        public override object Value
        {
            get
            {
                EnsureChildControls();
                return base.Value;
            }
            set
            {
                EnsureChildControls();
                base.Value = (String)value;
            }
        }
    }
}
